﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using CubicFWNet.Data.Models;
using CubicFWNet.Data.SystemManager;

namespace CubicFWNet.Application.SystemManager
{
    public class CreateNewNumberApp
    {
        private readonly PurchaseComposeRepository _servicePurchaseCompose = new PurchaseComposeRepository();
        private readonly WorksheetRepository _serviceWorksheet = new WorksheetRepository();
        private readonly TechnologyRepository _serviceTechnology = new TechnologyRepository();
        private readonly InvoiceRepository _serviceInvoice = new InvoiceRepository();
        /// <summary>
        /// 获得最新的上一个订单号(采购)
        /// </summary>
        /// <param name="number"></param>
        /// <returns></returns>
        public Invoice GetLastInvoiceCheck(string number)
        {
            return _serviceInvoice.IQueryable(a => a.Number.Contains(number)).OrderByDescending(a => a.CreateTime).FirstOrDefault();
        }

        /// <summary>
        /// 获得最新的上一个订单号(采购)
        /// </summary>
        /// <param name="number"></param>
        /// <returns></returns>
        public PurchaseCompose GetLastPurchaseCompose(string number)
        {
            return _servicePurchaseCompose.IQueryable(a => a.Number.Contains(number)).OrderByDescending(a => a.CreateTime).FirstOrDefault();
        }

        /// <summary>
        /// 获得最新的上一个订单号(生产)
        /// </summary>
        /// <param name="number"></param>
        /// <returns></returns>
        public Worksheet GetLastWorksheet(string number)
        {
            return _serviceWorksheet.IQueryable(a => a.Number.Contains(number)).OrderByDescending(a => a.CreateTime).FirstOrDefault();
        }

        //public int GetLastWorksheetCount(string number,int workType)
        public int GetLastWorksheetCount(string number)
        {
            //return _serviceWorksheet.IQueryable(a => a.Number.Contains(number)).ToList().Count();
            return _serviceWorksheet.GetLastWorksheetCount(number);
            //return _serviceWorksheet.GetLastWorksheetCount(number, workType);
        }

        /// <summary>
        /// 获得最新的上一个订单号(工艺)
        /// </summary>
        /// <param name="number"></param>
        /// <returns></returns>
        public Technology GetLasTechnology(string number)
        {
            return
                _serviceTechnology.IQueryable(a => a.Number.Contains(number))
                    .OrderByDescending(a => a.CreateTime)
                    .FirstOrDefault();
        }
    }
}
